package com.kayt.cyberBar.cpublic.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cpublic.business.dao.IPublicDepartDao;
import com.kayt.cyberBar.cpublic.business.entity.po.CPublicDepart;

@Repository("publicDepartDao")
public class PublicDepartDaoImpl implements IPublicDepartDao {
	@Autowired
	private EntityDao entityDao;
	
	@Override
	public CPublicDepart findByCode(String departCode) throws SQLException {
		if (StringUtils.isNotEmpty(departCode)) {
			List<CPublicDepart> departList = entityDao.getPagingDataBySql(CPublicDepart.class, -1, -1,
					"select * from c_public_depart where DEPART_CODE = '" + departCode + "'", null);
			if (departList != null && departList.size() == 1) {
				return departList.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	@Override
	public CPublicDepart findById(String departId) {
		if(StringUtils.isNotEmpty(departId)){
			Long id = null;
			try{
				id = Long.parseLong(departId);
			}catch (Exception e) {
				return null;
			}
			return entityDao.findById(id, CPublicDepart.class);
		}else{
			return null;
		}
	}

	@Override
	public Integer delete(String departId) {
		if(StringUtils.isNotEmpty(departId)){
			Long id = null;
			try{
				id = Long.parseLong(departId);
				CPublicDepart cPublicUserStatusCode = entityDao.findById(id, CPublicDepart.class);
				if(cPublicUserStatusCode != null){
					entityDao.delete(cPublicUserStatusCode);
					return 0;
				}else{
					return -1;
				}
			}catch (Exception e) {
				return -1;
			}
		}else{
			return -1;
		}
	}

	@Override
	public void modify(CPublicDepart entity) throws SQLException {
		entityDao.update(entity);
	}

	@Override
	public PageData<CPublicDepart> findByPage(String hql, Object[] params, LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<CPublicDepart> departPageData = this.entityDao.getPagingData(CPublicDepart.class, -1, -1, hql, params, orderMap);
		return departPageData;
	}

	@Override
	public PageData<CPublicDepart> getDepartList(LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<CPublicDepart> storePageData = this.entityDao.getPagingData(CPublicDepart.class, -1, -1, null, null, orderMap);
		return storePageData;
	}

	@Override
	public Long save(CPublicDepart entity) throws SQLException {
		return this.entityDao.save(entity);
	}

}
